package edu.chl.angholt.bookproject.client;

import edu.chl.angholt.bookproject.wsdl.Author;
import edu.chl.angholt.bookproject.wsdl.Book;
import edu.chl.angholt.bookproject.wsdl.View1;
import edu.chl.angholt.bookproject.wsdl.View1ImplService;
import java.util.List;
import java.util.Scanner;
import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;

/**
 * The main class for a simple example client of Web Servises
 * @author Patric Peterson
 */
public class App 
{
    
    /**
     * main method of the client.
     * @param args Not used.
     */
    public static void main( String[] args )
    {
        View1ImplService vs = new View1ImplService();
        View1 port = vs.getView1ImplPort();
        setHandler(port);   // seting up the handlers and interfacec
        boolean run = true;
        out("Connected to server.");
        Scanner in = new Scanner(System.in);  // statring a scaner
        while(run) {  // main loop
            out("What do you whant to do?");  //print manu
            out("1 : Search for author by name.");
            out("2 : Search for book by title.");
            out("3 : Search for author by id.");
            out("4 : Search for book by id.");
            out("0 : quit.");
            System.out.print(">");
            String anser = in.nextLine();
            out("--------------------------------");
            int i = new Integer(anser);
            switch(i){  // disid on an apropriat action
                case (0):
                    run = false;
                    break;
                case (1):
                    authorByName(in, port);
                    break;
                case (2):
                    bookByName(in, port);
                    break;
                case (3):
                    authorById(in, port);
                    break;
                case (4):
                    bookById(in, port);
                    break;
            }
            if (run){  // wait till user ready befor restarting enleas quiting
                System.out.print("----Hit enter to continu----");
                in.nextLine();
            }            
        }     
    }
    
    
    /**
     * Do all functions for getting and printing  and author that there name
     * @param in A scanner that the user will use
     * @param serv The interface to the Web Servises 
     */
    private static void authorByName(Scanner in,View1 serv) {
        System.out.print("name >");
        String name = in.nextLine().trim();
        Author au = serv.getAuthorByName(name);
        if(au == null)
            out("error");
        else
            printAuthor(au);
    }
    
    
    /**
     * Do all functions for getting and printing  and author that there name
     * @param in A scanner that the user will use
     * @param serv The interface to the Web Servises 
     */
    private static void authorById(Scanner in,View1 serv) {
        System.out.print("id >");
        Long id = new Long(in.nextLine().trim());
        Author au = serv.getAuthorById(id);
        if(au == null)
            out("error");
        else
            printAuthor(au);
    }
    
    /**
     * Print all information about author
     * @param au The author that to print
     */
    private static void printAuthor(Author au){
        out("name      : " + au.getName());
        out("biography : " + au.getBiography());
        out("country   : " + au.getCountry());
        out("site      : " + au.getWebSite());
        
    }
    
    /**
     * Print all information about book
     * @param bo The Book that to print
     */
    private static void printBook(Book bo) {
        out("title         : " + bo.getTitle());
        out("author        : " + bo.getAuthor().getName());
        out("publisher     : " + bo.getPublisher());
        out("synopsis      : " + bo.getSynopsis());
        out("first release : " + bo.getFirstRelease().getYear());
    }
    
    /**
     * Do all functions for getting and printing  and book that there name
     * @param in A scanner that the user will use
     * @param serv The interface to the Web Servises 
     */
    private static void bookByName(Scanner in,View1 serv) {
        System.out.print("name >");
        String name = in.nextLine().trim();
        Book bo = serv.getBookByTitle(name);
        if(bo == null)
            out("error");
        else
            printBook(bo);
    }
    
    /**
     * Do all functions for getting and printing  and book that there name
     * @param in A scanner that the user will use
     * @param serv The interface to the Web Servises 
     */
    private static void bookById(Scanner in,View1 serv) {
        System.out.print("id >");
        long id = new Long(in.nextLine().trim());
        Book bo = serv.getBookById(id);
        if(bo == null)
            out("error");
        else
            printBook(bo);
    }
    
    /**
     * Raper for print to System out
     * @param text Text to print
     */
    private static void out(String text) {
        System.out.println(text);
    }
    
    /**
     * set the handler for the web services port
     * @param port the interface of web services
     */
    private static void setHandler(View1 port) {
        Binding binding = ((BindingProvider) port).getBinding();
        List<Handler> handlerList = binding.getHandlerChain();
        // Add my SOAP handler
        handlerList.add(new MessageHandler() {});
        binding.setHandlerChain(handlerList);
    }
}
